"""
Case Type   : ustore copy from/to
Case Name   : utsore-copy 常用的基本类型测试
Create At   : 2024-07-25
Owner       : songjing20
Description :
    1.创建ustore表
    2.插入数据
    3.执行copy to操作
    4.执行copy from操作
    5.清理环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.成功
    5.成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class UstoreCopy(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.node_pri = Node(node='PrimaryDbUser')
        self.constant = Constant()
        self.tb_name = 't_copy_0002'
        self.file_abspath = os.path.join(
            macro.DB_BACKUP_PATH, f"{self.tb_name}.txt")

    def test_copy_from_to(self):
        text = '-----step1:创建ustore表;expect:成功-----'
        self.log.info(text)
        sql = f'''drop table if exists {self.tb_name} cascade;
        create table {self.tb_name}(c_integer integer,c_tinyint tinyint,
        c_smallint smallint,c_binary_integer binary_integer,
        c_bigint bigint,c_numeric numeric(10,4),c_number number(10,4),
        c_smallserial smallserial,c_serial serial,c_bigserial bigserial,
        c_real real,c_float float(3),c_double_precision double precision,
        c_binary_double binary_double,c_dec dec(10,3),
        c_integer1 integer(6,3),c_money money,c_boolean boolean,
        c_char char(10),c_varchar varchar(20),c_varchar2 varchar2(20),
        c_nvarchar2 nvarchar2(10),c_clob clob,c_text text,c_name name,
        c_char1 "char",c_blob blob,c_raw raw,c_date date,
        c_time time without time zone ,c_time1 time with time zone,
        c_time2 timestamp without time zone,
        c_time3 timestamp with time zone,
        c_time4 interval day(3) to second (4),
        c_time5 interval year (6),c_point point,c_box box,c_path path,
        c_circle circle,c_inet inet,c_bit bit(10),c_tsvector tsvector,
        c_tsquery tsquery,c_uuid uuid)with(storage_type=ustore);'''
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertEqual(4, sql_res.count(self.constant.CREATE_TABLE_SUCCESS),
                         f'执行失败:{text}')

        text = '-----step2:插入数据;expect:成功-----'
        self.log.info(text)
        sql = f"""insert into {self.tb_name} values(1,10,2,5,20,123456.122331,
        123456.12233,default,default,default,10.365456,123456.1234,
        321.321,10.365456,123.123654,123.1236547,25.98,'yes','数据库',
        '学习数据库','设计','工程师','测试','测试呀','视图','a',
        empty_blob(),'deadbeef','11-20-2020','21:21:21','21:21:21 pst',
        '2010-12-12','2013-12-11 pst',interval '3' day,interval '2' year,
        point '(2.0,0)',box '((0,0),(1,1))',path'((1,0),(0,1),(-1,0))',
        circle '((0,0),10)','192.168.1.14',b'1011111100',
        to_tsvector('english', 'the fat rats'),to_tsquery('fat:ab & cats'),
        'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11');"""
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_res,
                      f'执行失败:{text}')

        text = '-----step3:执行copy to操作;expect:成功-----'
        self.log.info(text)
        copy_to_res = self.pri_sh.execut_db_sql(
            f"copy {self.tb_name} to '{self.file_abspath}';")
        self.log.info(copy_to_res)
        self.assertIn('COPY 1', copy_to_res, f'执行失败:{text}')

        text = '-----step4:执行copy from操作;expect:成功-----'
        self.log.info(text)
        copy_from_res = self.pri_sh.execut_db_sql(
            f"copy {self.tb_name} from '{self.file_abspath}';")
        self.log.info(copy_from_res)
        self.assertIn('COPY 1', copy_from_res, f'执行失败:{text}')

    def tearDown(self):
        text = '-----step5:清理环境;expect:成功-----'
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            f"drop table if exists {self.tb_name} cascade;")
        self.log.info(drop_res)
        cmd = f'rm -f {self.file_abspath};echo $?;'
        self.log.info(cmd)
        cmd_res = self.node_pri.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, drop_res,
                      f'删表失败:{text}')
        self.assertEqual('0', cmd_res, f'执行失败:{text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
